/**
 * yiqihao(一起好商务股份有限公司) copyright 
 */
package com.p2p.front.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.p2p.base.cache.Cache;
import com.p2p.base.model.Money;
import com.p2p.front.dal.model.Tender;
import com.p2p.front.dal.model.User;
import com.p2p.front.dal.model.UserCredits;
import com.p2p.front.enums.UserCreditsType;
import com.p2p.front.service.TenderAmountSumService;
import com.p2p.front.service.TenderService;
import com.p2p.front.service.UserCreditsService;
import com.p2p.front.service.UserService;

/**
 *
 * @author leiruiqi@yiqihao.com
 *
 * 2015年4月13日 上午9:00:09
 */

@Service("tenderAmountSumService")
public class TenderAmountSumServiceImpl implements TenderAmountSumService {

	@Resource
	private Cache defaultCache;
	
	@Resource
	private TenderService tenderService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private UserCreditsService userCreditsService;
	
	@Override
	public void sumAmountForTotal(Tender tender) {
		String userId = String.valueOf(tender.getUid());
		UserCredits userCredits =userCreditsService.getByUserAndType(String.valueOf(tender.getUid()), UserCreditsType.UserCreditsTotalTenderAmount);
		if(userCredits==null){
			Money totalAmount = tenderService.sumTatalAmountByUid(userId);
			userCreditsService.create(userId, UserCreditsType.UserCreditsTotalTenderAmount, totalAmount.getCent());
		}else{
			userCreditsService.addValue(userId, UserCreditsType.UserCreditsTotalTenderAmount, tender.getAmount().getCent());
		}
	}

	@Override
	public Map<String, UserCredits> getUserTenderTatalAmount(List<String> userIds) {
		Map<String, UserCredits> map = userCreditsService.queryByTypeAndUids(userIds, UserCreditsType.UserCreditsTotalTenderAmount);
		List<String> needInitUserList = new ArrayList<String>();
		for(String userId:userIds){
			if(map.get(userId)==null){
				needInitUserList.add(userId);
			}
		}
		if(needInitUserList.size()>0){
			for(String userId:needInitUserList){
				
				//计算并创建
				Money totalAmount = tenderService.sumTatalAmountByUid(userId);
				userCreditsService.create(userId, UserCreditsType.UserCreditsTotalTenderAmount, totalAmount.getCent());
				
				//包装并放进map
				UserCredits userCredits = new UserCredits();
				userCredits.setUid(userId);
				userCredits.setType(UserCreditsType.UserCreditsTotalTenderAmount.name());
				userCredits.setValue(totalAmount.getCent());
				map.put(userId, userCredits);
			}
		}
		return map;
	}

	@Override
	public List<Map<String,Object>> queryTopTotalAmount() {
		String topKey = "Top_Total_Tender_amount_";
		Object object = defaultCache.getValue(topKey);
		
		List<Map<String,Object>> topList;
		if(object!=null){
			topList = (List<Map<String,Object>>)object;
		}
		else{
			topList = queryTop10TotalTenderAmount();
			defaultCache.add(topKey, topList, 3600);
		}
		return topList;
		
	}
	
	public List<Map<String,Object>> queryTop10TotalTenderAmount(){
		List<UserCredits> topList = userCreditsService.queryTopByType(UserCreditsType.UserCreditsTotalTenderAmount, 10);
		
		List<String> uidList = new ArrayList<String>();
		for(UserCredits userCredits:topList){
			uidList.add(userCredits.getUid());
		}
		
		Map<String,User> userMap = userService.searchUserByIdList(uidList);
		
		List<Map<String,Object>> topMapList = new ArrayList<Map<String,Object>>();
		
		for(UserCredits userCredits:topList){
			String uname = userMap.get(userCredits.getUid()).getUname();		
			Map<String,Object> tempData = new HashMap<String,Object>();
			tempData.put("uname", uname);
			tempData.put("totalAmount", new Money(userCredits.getValue()).toString());
			topMapList.add(tempData);
		}
		return topMapList;
	}

}
